package com.yaoyong.yy_demo.module.spring5.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect//1.声明一个切面
@Component//2.交给spring管理
public class LogAspect {

    @Pointcut("@annotation(com.yaoyong.yy_demo.module.spring5.aop.Action)")//3.声明切点
    public void annotationPointCut() {
    }

    @After("annotationPointCut()")//4.声明一个建言,并使用@Pointcut声明的切点
    public void after(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        Action action = method.getAnnotation(Action.class);
        //5.通过反射获得注解上的属性
        System.out.println("注解式拦截 " + action.name());
    }

    @Before("execution(* com.yaoyong.yy_demo.module.spring5.aop.DemoMethodService.*(..))")//6.声明一个建言,此建言直接使用拦截规则作为参数
    public void before(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        System.out.println("方法规则式拦截 " + method.getName());
    }
}
